package com.example.aspect;

import com.alibaba.fastjson.JSONObject;
import com.example.annotation.AccessLimit;
import com.example.response.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

@Component
public class SessionInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //判断请求是否是属于方法的请求
        if (handler instanceof HandlerMethod){
            HandlerMethod hm = (HandlerMethod) handler;
            //获取方法上的注解，看是否有该注解
            AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);
            if (null == accessLimit){
                return true;
            }
            int seconds = accessLimit.seconds();
            int maxCount = accessLimit.maxCount();
            //得到访问者的ip地址
            String ip = request.getRemoteAddr();
            //存放在redis中的key
            String key = request.getServletPath() + ":" + ip;
            Integer count = (Integer) redisTemplate.opsForValue().get(key);
            System.out.println(count);
            if (null == count || -1 == count) {
                // 第一次访问,设置过期时间
                redisTemplate.opsForValue().set(key, 1,seconds, TimeUnit.SECONDS);
                return true;
            }
            if (count < maxCount) {
                // count加1
                count = count + 1;
                redisTemplate.opsForValue().set(key, count,0);
                return true;
            }
            // 超出访问次数
            if (count >= maxCount) {
                // response 返回 json 请求过于频繁请稍后再试
                response.setCharacterEncoding("UTF-8");
                response.setContentType("application/json; charset=utf-8");
                Response result = new Response<>();
                result.setCode("23333333");
                result.setMsg("操作过于频繁,请稍后再试！");
                Object obj = JSONObject.toJSON(result);
                response.getWriter().write(JSONObject.toJSONString(obj));
                return false;
            }

        }
        return false;
    }
}
